package com.za.wbw.web.shop.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import com.za.wbw.model.shop.CommentInfo;
import com.za.wbw.model.shop.ProductInfo;
import com.za.wbw.service.shop.ProductService;
import com.za.wbw.service.shop.CommentService;
import com.za.wbw.util.BaseAction;
import com.za.wbw.util.StringUtil;

@SuppressWarnings("unchecked")
public class ProductAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	private ProductService productService;
	private CommentService commentService;
	// 查询产品列表
	public String queryProductList() throws Exception {
		Map<String,Object> _info = (Map<String, Object>)request.getSession().getAttribute("_info");
		Map<String,Object> pars = new HashMap<String,Object>();
		// 店铺ID
		int shopId = 0;
		if(!StringUtil.isNullOrEmpty(_info.get("shop_id"))){
			shopId = (Integer)_info.get("shop_id");
		}
		// 起始页
		int start = 0;
		if(!StringUtil.isNullOrEmpty(request.getParameter("start")))
		{
			start = Integer.parseInt(request.getParameter("start"));
		}
		pars.put("shopId",shopId);
		pars.put("start", start);
		// 状态
		String sstatusId = request.getParameter("statusId");
		if(!StringUtil.isNullOrEmpty(sstatusId)){
			pars.put("statusId", Integer.parseInt(sstatusId));
		}
		List<ProductInfo> list = getProductService().queryProductPage(pars);
		request.setAttribute("product_list", list);
		int count = getProductService().queryProductCount(pars);
		request.setAttribute("product_count", count);
		return SUCCESS;
	}
	// 查询产品信息
	public String queryProductInfo() throws Exception{
		int productId = Integer.parseInt(request.getParameter("productId"));
		// 产品信息
		ProductInfo info = getProductService().queryProductInfo(productId);
		request.setAttribute("product_info", info);
		Map<String, Object> pars = new HashMap<String, Object>();
		List<CommentInfo> list = getCommentService().queryCommentPage(pars);
		int count = getCommentService().queryCommentCount(pars);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("data", list);
		map.put("total", count);
		JSONArray json = JSONArray.fromObject(map);
		String commenJson = json.toString();
		request.setAttribute("comment_json", commenJson);
		return SUCCESS;
	}
	// 预添加产品信息
	public String preAddProductInfo() throws Exception{
		
		return SUCCESS;
	}
	// 添加产品信息
	public String addProductInfo() throws Exception{
		ProductInfo product = new ProductInfo();
		// 产品名称
		String productName = request.getParameter("productName");
		product.setProductName(productName);
		// 是否促销
		String sisDiscount = request.getParameter("isDiscount");
		int isDiscount = Integer.parseInt(sisDiscount);
		product.setIsDiscount(isDiscount);
		// 原价
		String ssellPrice = request.getParameter("sellPrice");
		double sellPrice = Double.parseDouble(ssellPrice);
		product.setSellPrice(sellPrice);
		// 现价
		if(isDiscount == 1)
		{
			String sdiscountPrice = request.getParameter("discountPrice");
			double discountPrice = Double.parseDouble(sdiscountPrice);
			product.setDiscountPrice(discountPrice);
		}
		else {
			product.setSellPrice(sellPrice);
		}
		// 描述
		String productBrief = request.getParameter("productBrief");
		product.setProductBrief(productBrief);
		// 保存
		product = getProductService().saveProductInfo(product);
		request.setAttribute("productId", product.getProductId());
		return SUCCESS;
	}
	// 预更新产品信息
	public String preUpdateProductInfo() throws Exception{
		int productId = Integer.parseInt(request.getParameter("productId"));
		ProductInfo product = getProductService().queryProductInfo(productId);
		request.setAttribute("product_info", product);
		return SUCCESS;
	}
	// 更新产品信息
	public String updateProductInfo() throws Exception{
		String sproductId = request.getParameter("productId");
		int productId = Integer.parseInt(sproductId);
		ProductInfo product = getProductService().queryProductInfo(productId);
		// 产品名称
		String productName = request.getParameter("productName");
		product.setProductName(productName);
		// 是否折扣
		String sisDiscount = request.getParameter("isDiscount");
		int isDiscount = Integer.parseInt(sisDiscount);
		product.setIsDiscount(isDiscount);
		// 原价
		String ssellPrice = request.getParameter("sellPrice");
		double sellPrice = Double.parseDouble(ssellPrice);
		product.setSellPrice(sellPrice);
		// 现价
		String sdiscountPrice = request.getParameter("discountPrice");
		double discountPrice = Double.parseDouble(sdiscountPrice);
		product.setDiscountPrice(discountPrice);
		// 产吕描述
		String productBrief = request.getParameter("productBrief");
		product.setProductBrief(productBrief);
		// 更新产品
		getProductService().updateProductInfo(product);
		// 设置
		request.setAttribute("productId", productId);
		return SUCCESS;
	}
	// 更新产品状态
	public String updateProductStatus() throws Exception{
		Map<String, Object> pars = new HashMap<String,Object>();
		// 产品编号
		String sproductId = request.getParameter("productId");
		int productId = Integer.parseInt(sproductId);
		pars.put("productId", productId);
		// 产品状态
		String sstatusId = request.getParameter("statusId");
		int statusId = Integer.parseInt(sstatusId);
		pars.put("statusId", statusId);
		// 更新
		getProductService().updateProductStatus(pars);
		// 返回值
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("status", "success");
		JSONArray json = JSONArray.fromObject(map);
		outPrint(json);
		return null;
	}
	// -----------------------------------------------
	public ProductService getProductService() {
		return productService;
	}
	public void setProductService(ProductService productService) {
		this.productService = productService;
	}
	public CommentService getCommentService() {
		return commentService;
	}
	public void setCommentService(CommentService commentService) {
		this.commentService = commentService;
	}
	
}
